package com.chidopi.cdf.struts1actions;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CdfDispatchAction extends MappingDispatchAction implements
		ICdfAction {
	private static Logger logger = LoggerFactory.getLogger(CdfDispatchAction.class);
	private String methodName;
	protected String entryURL;
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (isCancelled(request)) {
		    ActionForward af = cancelled(mapping, form, request, response);
		    if (af != null) {
		        return af;
		    }
		}
		// Get the parameter. This could be overridden in subclasses.
		String parameter = getParameter(mapping, form, request, response);

		// Get the method's name. This could be overridden in subclasses.
		String name = getMethodName(mapping, form, request, response, parameter);
		this.methodName = name;		// Jemmy add
		// Prevent recursive calls
		if ("execute".equals(name) || "perform".equals(name)){
		    String message = messages.getMessage("dispatch.recursive", mapping.getPath());
		    logger.error(message);
		    throw new ServletException(message);
		}
		// Invoke the named method, and return the result
		return dispatchMethod(mapping, form, request, response, name);
	}

	public String getMethodName() {
		return this.methodName;
	}
}
